package com.financing.platform.web;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.financing.platform.dto.DiscussDTO;
import com.financing.platform.dto.ProjectCommentsDTO;
import com.financing.platform.service.IDiscussService;
import com.financing.platform.service.IProjectCommentsService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.Dict;
import com.financing.platform.vo.ProjectCommentsVO;

@Controller
public class DiscussManageAction extends BaseAction{
	
	@Autowired
	private IDiscussService discussServiceImpl;
	@Autowired
	private IProjectCommentsService projectCommentsServiceImpl;
	
	@RequestMapping("/discussManage.shtml")
	public ModelAndView querySwitch(HttpServletRequest request,
			HttpServletResponse response ,DiscussDTO discussDTO) {
			Map<String,Object> resultMap = new HashMap<String,Object>();
			
			request.getSession().setAttribute(Dict.requestID, "discussManage.shtml");
			
			//判断是否需要修改评论状态 如果要则修改
			String[] discussId = request.getParameterValues(Dict.id);
			if(null != discussId && discussId.length> 0 ){
				try {
					DiscussDTO discussTemp = new DiscussDTO();
					discussTemp.setStatus(request.getParameter("updateStatus"));
					DTOdateSet.setUpdateInfo(discussTemp, request.getSession());
					discussServiceImpl.updateDiscuss(discussId,discussTemp);
					resultMap.put(Dict.result, Dict.result_success);
				} catch (SQLException e) {
					log.error("Update discuss status error!",e);
					//修改失败
					resultMap.put(Dict.result, Dict.result_error);
				}
			}
			
			//如果页面查询条件为空，则默认查询状态正常 评论类型为综合评论的评论
			if(discussDTO == null){
				discussDTO = new DiscussDTO();
				discussDTO.setStatus(Dict.discuss_status_normal);
				discussDTO.setuDiscussType(Dict.discuss_mixed_reviews);
			}
			if(discussDTO.getStatus() == null){
				discussDTO.setStatus(Dict.discuss_status_normal);
			}
			if(discussDTO.getuDiscussType() == null){
				discussDTO.setuDiscussType(Dict.discuss_mixed_reviews);
			}
			
			List<DiscussDTO> discussList = discussServiceImpl.discussList(discussDTO);
			
			resultMap.put("discussList", discussList);
			resultMap.put("discussDTO", discussDTO);
			
			return new ModelAndView("discussManage",resultMap);
	}
	
	@RequestMapping("/projectCommentsManage.shtml")
	public ModelAndView projectCommentsManage(HttpServletRequest request,
			HttpServletResponse response ,ProjectCommentsDTO projectCommentsDTO) {
			Map<String,Object> resultMap = new HashMap<String,Object>();
			
			request.getSession().setAttribute(Dict.requestID, "projectCommentsManage.shtml");
			
			//判断是否需要修改评论状态 如果要则修改
			String[] projectId = request.getParameterValues(Dict.id);
			if(null != projectId && projectId.length> 0 ){
				try {
					ProjectCommentsDTO projectCommentsDTOTmp = new ProjectCommentsDTO();
					projectCommentsDTOTmp.setStatus(request.getParameter("updateStatus"));
					DTOdateSet.setUpdateInfo(projectCommentsDTOTmp, request.getSession());
					projectCommentsServiceImpl.updateProjectComment(projectCommentsDTOTmp,projectId);
					resultMap.put(Dict.result, Dict.result_success);
				} catch (SQLException e) {
					log.error("Update discuss status error!",e);
					//修改失败
					resultMap.put(Dict.result, Dict.result_error);
				}
			}
			
			//如果页面查询条件为空，则默认查询状态正常 评论类型为综合评论的评论
			if(projectCommentsDTO == null){
				projectCommentsDTO = new ProjectCommentsDTO();
				projectCommentsDTO.setStatus(Dict.project_comments_status_normal);
			}
			if(projectCommentsDTO.getStatus() == null){
				projectCommentsDTO.setStatus(Dict.project_comments_status_normal);
			}
			
			if(projectCommentsDTO.getpCommentsType() == null){
				projectCommentsDTO.setpCommentsType(Dict.project_comments_type_platform);
			}
			List<ProjectCommentsVO> projectCommentsList = projectCommentsServiceImpl.queryProjectComments(projectCommentsDTO);
			
			resultMap.put("projectCommentsList", projectCommentsList);
			resultMap.put("projectCommentsDTO", projectCommentsDTO);
			
			return new ModelAndView("projectCommentsManage",resultMap);
	}
	
	
}
